//
//  NoteViewController.m
//  MyNote
//
//  Created by Adiljan on 2016/11/22.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "NoteViewController.h"
#import "AddViewController.h"

//#import "ContentItemCell.h"
#import "MyNoteCell.h"
//#import "MyNotesCell.h"
#import "GlobalConstant.h"


@interface NoteViewController ()

@property (nonatomic, strong )NSArray *arrModel;
@end

@implementation NoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initViews];
    [self initNavigation];
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    /*Adiljan 获取数据库数据*/
    [self getDataBase];
    [_tblList reloadData];
}
- (void)initNavigation{
    UIButton *btnAdd = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [btnAdd setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [btnAdd addTarget:self action:@selector(btnAddClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:btnAdd];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
-(void)initViews{
    self.view.backgroundColor = [UIColor blackColor];
    self.view.alpha = 0.7;
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.alpha = 0.7;
    UIAlmasLabel *lblTitle = [[UIAlmasLabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    lblTitle.font = FONTWITHSIZE(24);
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.text = NSLocalizedString(@"hatirem", nil);
    
    self.navigationItem.titleView = lblTitle;
    
    self.tblList.dataSource = self;
    self.tblList.delegate = self;
    [self.tblList registerNib:[UINib nibWithNibName:@"MyNoteCell" bundle:nil] forCellReuseIdentifier:@"MyNoteCell"];
    
    self.tblList.tableFooterView = [[UIView alloc]init];
    
}
/*Adiljan 获取数据库数据*/
- (void)getDataBase{
    NSString *strSql = @"SELECT * FROM t_modals;";
    NSArray *modals = [NoteTools queryData:strSql];
    
    self.arrModel = modals;
    // 数组倒序排序
    self.arrModel = (NSMutableArray *)[[self.arrModel reverseObjectEnumerator] allObjects];
}

/*Adiljan 跳转到添加页面*/
- (void)btnAddClick{
    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil];
    add.fromType = typeAdd;
    [self.navigationController pushViewController:add animated:YES];
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 获取cell
    MyNoteCell *cell = (MyNoteCell *)[tableView cellForRowAtIndexPath:indexPath];
    NoteModel *model = self.arrModel[indexPath.row];
    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil];
    add.fromType = typeUpdate;
    add.strContent = cell.lblContent.text;
    add.strCreateDate = cell.lblDate.text;
    add.strCreateTime = model.createTime;
    [self.navigationController pushViewController:add animated:YES];
}

#pragma mark tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
        return self.arrModel.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyNoteCell"];
    
    NoteModel *model = self.arrModel[indexPath.row];
    cell.lblContent.text = model.noteContent;
    cell.lblDate.text = model.createDate;
    cell.lblContent.font = FONTWITHSIZE(15);
    cell.lblDate.font = FONTWITHSIZE(10);
    cell.leftUtilityButtons = [self leftButtons];
    cell.delegate = self;
    cell.myDelegate = self;
    return cell;
    
}

// 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}
// 到AddViewController修改
- (void)myNoteCellItemClick:(MyNoteCell *)cell{
    
}


- (NSArray *)leftButtons
{
    NSMutableArray *leftUtilityButtons = [NSMutableArray new];
    [leftUtilityButtons sw_addUtilityButtonWithColor:
     [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                                title:NSLocalizedString(@"delete", nil)];
    return leftUtilityButtons;
}
#pragma mark - SWTableViewDelegate

- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
    switch (state) {
        case 0:
            NSLog(@"utility buttons closed");
            break;
        case 1:
            NSLog(@"left utility buttons open");
            break;
        case 2:
            NSLog(@"right utility buttons open");
            break;
        default:
            break;
    }
}

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index
{

    NoteModel *model = self.arrModel[index];
    switch (index) {
        case 0:
            if ([self deleteData:model.createTime]) {
                [self getDataBase];
                [self.tblList reloadData];
                // 关闭cell
                [cell hideUtilityButtonsAnimated:YES];
            }else{
                NSLog(@"Adiljan===>删除失败");
            }
            
            break;
       
        default:
            break;
    }
}
// 删除
-(BOOL)deleteData:(NSString *)time{

    NSString * strSql = [NSString stringWithFormat:@"delete from t_modals where time = '%@'",time];
    BOOL del = [NoteTools deleteData:strSql];
    return del;
}
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            NSLog(@"More button was pressed");
            
            [cell hideUtilityButtonsAnimated:YES];
            break;
        }
        
        default:
            break;
    }
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
    // allow just one cell's utility button to be open at once
    return YES;
}

- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
    switch (state) {
        case 1:
            // set to NO to disable all left utility buttons appearing
            return YES;
            break;
        case 2:
            // set to NO to disable all right utility buttons appearing
            return YES;
            break;
        default:
            break;
    }
    
    return YES;
}
@end
